<?php
/**
 * Created by ptmall
 * User : 河南邦耀网络科技有限公司
 * Date : 2020/9/4
 * Time : 16:26
 */

namespace app\api\controller;


use app\admin\model\Collect as Cm;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Collect extends Api
{
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * @Explain :收藏商品
     * @Date : 2020/9/4 16:59
     * @Author : 河南邦耀网络科技有限公司
     */
    public function collect(){
        $userInfo=$this->auth->getUser();
        $goodsId = $this->request->request("goodsId",'');
        $goodsId = explode(',',$goodsId);
        if((new Cm())->where('uid',$this->auth->id)->count()+count($goodsId)>=200){
            $this->error('最多收藏200个商品');
        }
        foreach ($goodsId as $v){
            $data=Cm::get(['uid'=>$userInfo->id,'goodsId'=>$v,'status'=>1]);
            if(!empty($data) || empty($v)) continue;
            try {
                (new Cm())->collect($userInfo->id,$v);
            }catch (Exception $exception){
                $this->error($exception->getMessage());
            }
        }
        $this->success('收藏成功');
    }

    /**
     * @Explain :获取收藏列表
     * @Date : 2020/9/4 17:29
     * @Author : 河南邦耀网络科技有限公司
     */
    public function getList2(){
        $cm=new Cm();
        $userInfo=$this->auth->getUser();
        $pn = $this->request->request('pn',1);
        $selfCateId=$this->request->request('selfCateId',0);
        $keyword = $this->request->request('keyword','');
        $condition=['uid'=>$userInfo->id];
        $condition['status']=1;
        if($selfCateId){
            $condition['selfCateId']=$selfCateId;
        }
        $collect=$cm->where($condition)->page($pn,10)->select();
        if(empty($collect)) $this->success('暂无收藏商品');
        $goodsIds=array_column($collect,'goodsId');
        $goodsModel=new \app\admin\model\goods\Goods();
        $condition=['goodsid'=>['in',$goodsIds]];
        if(!empty($keyword)){
            $condition['title']=['like',"%{$keyword}%"];
        }
        $data=$goodsModel->field(['goodsid','title','image','sales','price','selfCateId','status'])->where($condition)->select();
        $data=collection($data)->toArray();
        foreach ($data as &$row){
            foreach ($collect as $value){
                if($row['goodsid']==$value['goodsId']){
                    $row['createtime']=$value['ctime'];
                }
            }
        }
        $start=($pn-1)*10;
        $data = array_slice($data,$start,10);

        $this->success('ok',$data);
    }
    /**
     * 获取收藏列表
     */
    public function getList()
    {
        $pn = $this->request->request('pn',1);
        $keyword = $this->request->request('keyword','');
        $condition = '';
        if(!empty($keyword)){
            $condition = " AND b.title like '%{$keyword}%'";
        }
        $sql = "SELECT a.*,a.ctime as createtime,b.goodsid,b.title,b.image,b.sales,b.price,b.selfCateId,b.status,b.discount FROM `pt_collect` as a join `pt_goods` as b on a.goodsId=b.goodsid WHERE a.uid='{$this->auth->id}' AND a.status=1 $condition ORDER BY a.id desc LIMIT ". ($pn - 1) * $this->limit . "," . $this->limit;
        $list = Db::query($sql);
        $data = !empty($list)?$list:[];
        foreach ($data as &$row){
            $row['spellPrice']=bcmul($row['price'],$row['discount']*0.01,2);
        }
        $this->success('ok',$data);
    }

    /**
     * @Explain :删除收藏商品
     * @Date : 2020/9/4 16:59
     * @Author : 河南邦耀网络科技有限公司
     */
    public function remove(){
        $userInfo=$this->auth->getUser();
        $goodsIds = $this->request->request("goodsIds");
        $result=Cm::destroy(['uid'=>$userInfo->id,'goodsId'=>['in',explode(',',$goodsIds)]]);
        if($result){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }

    }



}
